'use strict'

import { app, BrowserWindow, globalShortcut } from 'electron'

let mainWindow
const winURL = process.env.NODE_ENV === 'development'
    ? `http://localhost:9080`
    : `file://${__dirname}/index.html`

function createWindow() {
    mainWindow = new BrowserWindow({
        width: 1000,
        height: 563,
        useContentSize: true,
        webPreferences: {
            nodeIntegration: true,
            enableRemoteModule: true,
            webSecurity: true
        }
    })

    mainWindow.loadURL(winURL)

    mainWindow.on('closed', () => {
        mainWindow = null
    })

    console.log('__static:', __static)
}

/**
 * 注册快捷键
 */
function registerGlobalShortcut() {
    // 打开控制台
    globalShortcut.register('Alt+X', () => {
        if (mainWindow) {
            mainWindow.webContents.openDevTools({ mode:'detach' });
        }
    });
}

app.on('ready', () => {
    try {
        createWindow()

        if (process.env.MODE !== 'prod') {
            registerGlobalShortcut()
        }
    } catch (error) {
        setTimeout(() =>　app.quit(), 10)
        throw error
    }
})

app.on('window-all-closed', () => {
    globalShortcut.unregisterAll()

    if (process.platform !== 'darwin') {
        app.quit()
    }
})

app.on('activate', () => {
    if (mainWindow === null) {
        createWindow()
    }
})

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
